package Eyjafjalla.action;

import Eyjafjalla.helper.EyjafjallaHelper;
import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.UIStrings;

import java.util.Iterator;

public class ChooseDiscardToCloudAction extends AbstractGameAction {
    private static final UIStrings uiStrings;
    public static final String[] TEXT;
    private AbstractPlayer p;
    private boolean isRandom;
    private boolean endTurn;
    private boolean canPickZero;
    private int fire = 0;
    public static int numClouded;
    private static final float DURATION;
    boolean hasSelect = false;

    public ChooseDiscardToCloudAction(int amount, int fire, boolean isRandom) {
        this(amount,fire,true, isRandom, false);
    }

    public ChooseDiscardToCloudAction(int amount, int fire,boolean canPickZero, boolean isRandom, boolean endTurn) {
        this.p = AbstractDungeon.player;
        this.isRandom = isRandom;
        this.setValues(null, null, amount);
        this.actionType = ActionType.CARD_MANIPULATION;
        this.endTurn = endTurn;
        this.canPickZero = canPickZero;
        this.duration = DURATION;
        this.fire = fire;
    }

    public void update() {
        if (this.duration == DURATION) {
            if (AbstractDungeon.getMonsters().areMonstersBasicallyDead()) {
                this.isDone = true;
                return;
            }

            int size;
            if (!canPickZero&&this.p.discardPile.size() <= this.amount&&(this.p.discardPile.size()+CloudPatch.cloudGroup.size())<=5) {
                this.amount = this.p.discardPile.size();
                size = this.p.discardPile.size();

                for(int i = 0; i < size; ++i) {
                    AbstractCard c = this.p.discardPile.getTopCard();
                    if(fire>0)
                        EyjafjallaHelper.IncreaseCard(c,fire,fire,0,0);
                    CloudPatch.moveToCloud(this.p.discardPile,c);
                }

                AbstractDungeon.player.hand.applyPowers();
                this.tickDuration();
                return;
            }

            if (!this.isRandom) {
                if (this.amount < 0) {
                    CardGroup tmp = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);
                    tmp.group.addAll(this.p.discardPile.group);
                    AbstractDungeon.gridSelectScreen.open(tmp,99,true,TEXT[4]);
                    this.tickDuration();
                    return;
                }
                if(this.amount > 5-CloudPatch.cloudGroup.size())
                    this.amount = 5-CloudPatch.cloudGroup.size();
                if(this.amount > p.discardPile.size())
                    this.amount = p.discardPile.size();
                numClouded = this.amount;
                if (this.amount>0) {
                    CardGroup tmp = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);
                    tmp.group.addAll(this.p.discardPile.group);
                    AbstractDungeon.gridSelectScreen.open(tmp,this.amount,this.canPickZero,TEXT[2]+this.amount+TEXT[3]);
                }
                else {
                    CloudPatch.createCloudFullDialog();
                    this.isDone = true;
                    return;
                }

                this.tickDuration();
                return;
            }

            for(int i = 0; i < this.amount&&CloudPatch.cloudGroup.size()<5; ++i) {
                AbstractCard c = this.p.discardPile.getRandomCard(AbstractDungeon.cardRandomRng);
                if(fire>0)
                    EyjafjallaHelper.IncreaseCard(c,fire,fire,0,0);
                CloudPatch.moveToCloud(this.p.discardPile,c);
            }
        }

        if (!hasSelect) {
            Iterator var4 = AbstractDungeon.gridSelectScreen.selectedCards.iterator();

            while(var4.hasNext()) {
                AbstractCard c = (AbstractCard)var4.next();
                if(CloudPatch.cloudGroup.size()<5){
                    if(fire>0)
                        EyjafjallaHelper.IncreaseCard(c,fire,fire,0,0);
                    CloudPatch.moveToCloud(this.p.discardPile,c);
                }
            }
            AbstractDungeon.gridSelectScreen.selectedCards.clear();
            hasSelect = true;
        }

        this.tickDuration();
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_OPERATION);
        TEXT = uiStrings.TEXT;
        DURATION = Settings.ACTION_DUR_XFAST;
    }
}


